using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Drawing.Imaging;
using System.Text;
using System.Windows.Forms;

namespace monkeyLand
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Point monkeyPosition = new Point(0,0);
        Point rectangle = new Point(0, 0);
        string path;
        //int gravity = 1;
        
        private void panel1_GotFocus(object sender, EventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            path = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].FullyQualifiedName;
            path = path.Replace(System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0].Name, "");
        }
        private void DrawBackground()
        {
            string backgroundPath = path + "background.jpg";
            Bitmap bg = new Bitmap(backgroundPath);

            Graphics g = CreateGraphics();

            g.DrawImage((System.Drawing.Image)(bg), 0, 0);
        }
        private void DrawMonkey(int x, int y)
        {
            if (bmpmonkey == null)
            {
                string monkeyPath = path + "monkey.bmp";
                bmpmonkey = new Bitmap(monkeyPath);

                Graphics g = CreateGraphics();
                ImageAttributes attrib = new ImageAttributes();
                Rectangle rec = new Rectangle(x, y, 63, 44);
                attrib.SetColorKey(bmpmonkey.GetPixel(0, 1), bmpmonkey.GetPixel(0, 1));

                g.DrawImage((System.Drawing.Image)(bmpmonkey), rec, x, y, 63, 44, GraphicsUnit.Pixel, attrib);
                Graphics.FromImage(bmpmonkey);////////////////////them vao bo nho may
                
            }
           
        }
        

        private void timer1_Tick(object sender, EventArgs e)
        {
            Point position = new Point();
            position.X = monkeyPosition.X;
            position.Y = monkeyPosition.Y + 1;// * gravity);

            rectangle.X = monkeyPosition.X;
            rectangle.Y = monkeyPosition.Y + 1;
            //if(0==CheckInForm(position))
            //    return;
            
            monkeyPosition = position;
            Invalidate();
            //gravity = gravity * 2;
        }
        protected override void OnPaint(PaintEventArgs e)
        {
            
            //////////////////
            CreateObject();
            e.Graphics.DrawImage(bg, 0, 0);
            
                        
            ImageAttributes attrib = new ImageAttributes();
            Rectangle rec = new Rectangle(monkeyPosition.X,monkeyPosition.Y, 63, 44);
            attrib.SetColorKey(bmpmonkey.GetPixel(0, 1), bmpmonkey.GetPixel(0, 1));

            e.Graphics.DrawImage(bmpmonkey, rec, 0,1, 63, 44, GraphicsUnit.Pixel, attrib);
           // base.OnPaint(e);
        }
        protected override void OnPaintBackground(PaintEventArgs e)
        {
         //   base.OnPaintBackground(e);
        }
        Bitmap bg=null;
        Bitmap bmpmonkey = null;
        private void CreateObject()
        {
            if (bg == null)
            {
                string backgroundPath = path + "background.jpg";
                bg = new Bitmap(backgroundPath);
                Graphics g = CreateGraphics();
                g.DrawImage((System.Drawing.Image)(bg), 0, 0);
                Graphics.FromImage(bg);

                string monkeyPath = path + "monkey.bmp";
                bmpmonkey = new Bitmap(monkeyPath);

                
                ImageAttributes attrib = new ImageAttributes();
                Rectangle rec = new Rectangle(0, 1, 63, 44);
                attrib.SetColorKey(bmpmonkey.GetPixel(0, 1), bmpmonkey.GetPixel(0, 1));

                g.DrawImage((System.Drawing.Image)(bmpmonkey), rec, 0, 1, 63, 44, GraphicsUnit.Pixel, attrib);
                Graphics.FromImage(bmpmonkey);////////////////////them vao bo nho may
                
            }
        }
        private int CheckInForm(Point position)
        {
            // return 0 if monkey not in form
            //        1 if monkey in form

            //check position X of monkey 
            if (240 == position.X || position.X < 0)//+ 63)
            {
                timer1.Enabled = false;
                MessageBox.Show("you are died!!!");                
                return 0;
            }
            //check position Y of monkey 
            if (268 == position.Y || position.Y < 0)//+ 44)
            {
                timer1.Enabled = false;
                MessageBox.Show("be Ly khoc nhe!!!");                
                return 0;
            }
            return 1;
        }

        private void picBackground_Click(object sender, EventArgs e)
        {
            

        }

        private void Form1_KeyDown(object sender, KeyEventArgs e)
        {//nho lam them tron cho monkey move
            if ((e.KeyCode == System.Windows.Forms.Keys.Up))
            {
                // Rocker Up
                // Up
                monkeyPosition.Y -= 2;
                //MonkeyMove(monkeyPosition);
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Down))
            {
                // Rocker Down
                // Down
                // Do not thing
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Left))
            {
                // Left
                monkeyPosition.X -= 1;
                //monkeyPosition.Y -= 1;
                //MonkeyMove(monkeyPosition);
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Right))
            {
                // Right
                monkeyPosition.X += 1;
                //monkeyPosition.Y -= 1;
                //MonkeyMove(monkeyPosition);
            }
            if ((e.KeyCode == System.Windows.Forms.Keys.Enter))
            {
                // Enter
            }

        }

        private void panel1_GotFocus_1(object sender, EventArgs e)
        {

        }

        
    }
}